package com.itheima.controller;

import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import com.itheima.utils.R;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam("ids") List<Long> ids){

        boolean isdelete = addressBookService.removeByIds(ids);


        return isdelete?R.success("删除成功"):R.error("删除失败");
    }


    /**
     * 修改地址
     */
    @PutMapping
    public R<AddressBook> update(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }


    /**
     * 新增地址
     */
    @PostMapping()
    public R<Object> add(@RequestBody AddressBook addressBook, HttpServletRequest request){
        Long id =(Long) request.getSession().getAttribute("user");
        addressBook.setUserId(id);
        Integer result = addressBookService.add(addressBook);

        return result == null ? R.error("添加地址失败") : R.success("添加地址成功");
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    public R<Object> setDefault(@RequestBody AddressBook addressBook,HttpServletRequest request){

        Long userId =(Long) request.getSession().getAttribute("user");
         addressBook.setUserId(userId);
        Integer result = addressBookService.setDefault(addressBook);

        return result == null ? R.error("设置默认地址失败") : R.success("设置默认地址成功");
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R<Object> get(@PathVariable Long id){
        AddressBook addressBook = addressBookService.get(id);

       return addressBook == null ? R.error("没有找到该对象") : R.success(addressBook);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<Object> getDefault(HttpServletRequest request){

        Long user =(Long) request.getSession().getAttribute("user");
        System.out.println(user);
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(user);

        AddressBook aDefault = addressBookService.getDefault(addressBook);

    return aDefault == null ? R.error("查询失败") : R.success(aDefault);
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public  R<Object>  list(AddressBook addressBook,HttpServletRequest request){
        Long user = (Long) request.getSession().getAttribute("user");

        addressBook.setUserId(user); //设置用户id

        List<AddressBook> listAddressBook = addressBookService.list(addressBook);

        return listAddressBook == null ? R.error("查询失败") : R.success(listAddressBook);
    }


}
